/*!
 * \file	menu.c
 * \brief	Definitions of game menu functions.
 * \author	Jon "delrio" Cox
 * \date	February 2, 2011
 */

//------------------------------------------------------------------------//
//  Qong                                                                  //
//  Copyright (C) 2011 Jon Cox                                            //
//                                                                        //
//  This program is free software: you can redistribute it and/or modify  //
//  it under the terms of the GNU General Public License as published by  //
//  the Free Software Foundation, either version 3 of the License, or     //
//  (at your option) any later version.                                   //
//                                                                        //
//  This program is distributed in the hope that it will be useful,       //
//  but WITHOUT ANY WARRANTY; without even the implied warranty of        //
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
//  GNU General Public License for more details.                          //
//                                                                        //
//  You should have received a copy of the GNU General Public License     //
//  along with this program.  If not, see <http://www.gnu.org/licenses/>. //
//------------------------------------------------------------------------//

#include <SDL.h>
#include <defines.h>
#include <types.h>

/*!
 * \brief	The main menu.
 * \return	Menu selection item.
 */
mSelect_t menuMain()
{
	unsigned short int pos = 0;
	mSelect_t choice = m_NONE;
	mSelect_t list[4] = { m_SINGLE, m_SETTINGS, m_CREDITS, m_QUIT };
	SDL_Event event;

	// TODO: SHOW MAIN MENU
	while ( choice == m_NONE )
	{
		SDL_WaitEvent( &event );
		switch ( event.type )
		{
			// TODO: Menu navigation.
			case SDL_KEYDOWN:
				switch ( event.key.keysym.sym )
				{
					case SDLK_DOWN:
						if ( pos >2 )
						{
							pos = 0;
						}
						else
						{
							pos++;
						}
						// TODO: Update menu display.
						break;
					case SDLK_UP:
						if ( pos < 1 )
						{
							pos = 3;
						}
						else
						{
							pos--;
						}
						// TODO: Update menu display.
						break;
					case SDLK_RETURN:
						// I've made a decision!
						choice = list[pos];
						break;
				}
				break;
			case SDL_QUIT:
				choice = m_FORCEQUIT;
				break;
		}

		// Exit menu if the user did anything of interest.
		if ( choice != m_NONE )
		{
			return choice;
		}
	}

	return m_NONE;
}

/*!
 * \brief	Menu to confirm whether or not a user wants to quit.
 * \return	m_YES or m_NO depending on user choice.
 */
mSelect_t menuConfirmQuit()
{
	unsigned int pos = 1;
	mSelect_t choice = m_NONE;
	mSelect_t list[2] = { m_YES, m_NO };
	SDL_Event event;

	// TODO: Show quit confirm menu.
	while ( choice == m_NONE )
	{
		SDL_WaitEvent( &event );
		switch ( event.type )
		{
			case SDL_KEYDOWN:
				switch ( event.key.keysym.sym )
				{
					case SDLK_DOWN:
					case SDLK_UP:
						pos = 1 ^ pos;
						// TODO: Update menu.
						break;
					case SDLK_RETURN:
						choice = list[pos];
						break;
				}
				break;
			// TODO: Menu navigation.
			case SDL_QUIT:
				choice = m_FORCEQUIT;
				break;
		}
		
		if ( choice != m_NONE )
		{
			return choice;
		}
	}

	return m_NONE;
}
